package biz

import (
	"cyiris/core/core"
	"context"
	"fmt"

	"github.com/go-redis/redis/v8"
)

type RedisBiz struct {
}

func NewRedisBiz() *RedisBiz {
	return &RedisBiz{}
}

func (biz *RedisBiz) Handle(member string) ([]string, error) {
	redisClient, err := core.GetRedis("default")
	if nil != err {
		return nil, err
	}

	setKey := "cyiris-test-set"
	ctx := context.Background()

	err = redisClient.SAdd(ctx, setKey, member).Err()
	if nil != err {
		return nil, err
	}

	members, err := redisClient.SMembers(ctx, setKey).Result()
	if err == redis.Nil {
		return nil, fmt.Errorf("%s does not exist", setKey)
	} else if err != nil {
		return nil, err
	}

	return members, err
}
